package likou;

/**
 * @author: 唐小尊
 * @email: 1171702529@qq.com
 * @cate: 2021/08/21 15:09
 */
public class _309 {
    public static void main(String[] args) {
        System.out.println(maxProfit(new int[]{1,2,3,0,2}));
    }
    public static int maxProfit(int[] prices) {
        int[] states = new int[3];//0买入，1卖出，2冷冻
        /**
         * states[0] = 当前买入最优解 = Math.max(当前买入最优解,当前冷冻最优解-当前价格)
         * states[1] = 当前卖出最优解 = Math.max(当前卖出最优解,当前买入最优解+当前价格)
         * states[2] = 当前冷冻最优解 = Math.max(当前冷冻最优解,上次卖出价格)
         *
         */
        states[0] = -prices[0];
        for (int price:prices){
            int pre1 = states[1];
            states[0] = Math.max(states[0],states[2]-price);
            states[1] = Math.max(states[1],states[0]+price);
            states[2] = Math.max(states[2],pre1);
        }
        return states[1];
    }
}
